<template>
    <view>
        <view
            v-for="item in children"
            :key="item.id"
            :class="{ rootDefaultStyle: !item.type }"
        >
            <!-- view类型 -->
            <view :style="[$u.addStyle(item.style)]" v-if="item.type == 'view'">
                <!-- 如果view下面还有contents就继续往下嵌套（递归调用当前组件） -->
                <!-- :style="[$u.addStyle(item.style)]" -->
                <RecursiveComponent
                    :meetId="meetId"
                    :status="status"
                    :componentsData="componentsData"
                    :children="item.contents"
                    v-if="item.contents && item.contents.length > 0"
                />
            </view>

            <!-- 文本类型 -->
            <view
                v-if="item.type == 'text' || item.type == 'textarea'"
                :style="[$u.addStyle(item.style)]"
            >
                <u-text
                    :text="getComponentData(item.id).config.value"
                    :color="item.style.color"
                    :size="item.style.fontSize"
                    :align="item.style.textAlign || 'left'"
                    :bold="item.style.fontWeight == 'bold'"
                    :lineHeight="item.style.lineHeight"
                />
            </view>

            <!-- 单张图片类型 -->
            <view
                :style="[$u.addStyle(item.style)]"
                v-if="item.type === 'image'"
            >
                <u-image
                    :src="getComponentData(item.id).config.value"
                    :width="item.attrs.width"
                    :height="item.attrs.height"
                />
            </view>

            <!-- 多张图片 -->
            <scroll-view
                scroll-y
                class="imagesScrollView"
                :style="[$u.addStyle(item.style)]"
                v-if="item.type === 'images'"
            >
                <view
                    class="imagesScrollView_item"
                    :key="imageIndex"
                    v-for="(imageItem, imageIndex) in getComponentData(item.id)
                        .config.value"
                >
                    <u-image
                        :width="item.attrs.imageWidth"
                        :height="item.attrs.imageHeight"
                        :src="imageItem.url"
                        @click="preview(item.id, imageIndex)"
                    />
                </view>
            </scroll-view>

            <!-- 地图 -->
            <view
                class="map"
                v-if="item.type === 'navigation'"
                :style="[$u.addStyle(item.style)]"
            >
                <map
                    show-location
                    :enable-scroll="false"
                    :enable-zoom="false"
                    :scale="17"
                    :markers="[
                        {
                            id: 1,
                            width: markerWidth,
                            height: markerHeight,
                            label: {
                                content: getComponentData(item.id).config.value,
                                textAlign: 'center',
                                fontSize: mapLableFontSize,
                                color: '#666',
                            },
                            latitude: getComponentData(item.id).config.latitude,
                            longitude: getComponentData(item.id).config
                                .longitude,
                        },
                    ]"
                    style="width: 100%; height: calc(100% + 40rpx)"
                    :latitude="getComponentData(item.id).config.latitude"
                    :longitude="getComponentData(item.id).config.longitude"
                />
                <view
                    class="map_button"
                    @click="
                        goMap({
                            name: getComponentData(item.id).config.value,
                            latitude: getComponentData(item.id).config.latitude,
                            longitude: getComponentData(item.id).config
                                .longitude,
                        })
                    "
                >
                    <view>
                        <u-icon
                            label="开始导航"
                            labelSize="28rpx"
                            space="8rpx"
                            labelColor="#FFF"
                            width="32rpx"
                            height="32rpx"
                            labelPos="right"
                            name="https://test-health-s.oss-cn-hangzhou.aliyuncs.com/report/other/2024/01/25/086c1db923944ec7a6babc58827df28d.png"
                        />
                    </view>
                </view>
            </view>

            <view
                class="line"
                v-if="item.type === 'divider'"
                :style="[
                    $u.addStyle(item.style),
                    { backgroundColor: item.attrs.lineColor },
                    {
                        width:
                            item.attrs.mode === 'column'
                                ? item.attrs.lineWeight
                                : item.attrs.lineLength,
                        height:
                            item.attrs.mode === 'column'
                                ? item.attrs.lineLength
                                : item.attrs.lineWeight,
                    },
                ]"
            />

            <!-- 横向||纵向filed类型（key:value固定布局） -->
            <!-- { flexDirection: item.attrs.mode } -->
            <view
                class="filed"
                v-if="item.type == 'field'"
                :style="[$u.addStyle(item.style)]"
            >
                <view
                    class="filed_item"
                    :style="{ flexDirection: item.attrs.mode }"
                    v-for="fieldItem in getComponentData(item.id).config.value"
                    :key="fieldItem.id"
                >
                    <view
                        v-if="item.attrs.mode === 'column'"
                        class="filed_item_line"
                        :style="{ background: item.style.color }"
                    />
                    <text
                        :style="{
                            fontWeight:
                                item.attrs.mode === 'row' ? 'normal' : 'bold',
                            fontSize: item.attrs.labelSize,
                            marginBottom:
                                item.attrs.mode === 'column' ? '12rpx' : 0,
                        }"
                    >
                        {{ fieldItem.label }}
                    </text>
                    <text :style="{ fontSize: item.attrs.textSize }">{{
                        fieldItem.text
                    }}</text>
                </view>
            </view>

            <!-- step条 -->
            <view
                v-if="item.type === 'timeline'"
                class="step"
                :style="[$u.addStyle(item.style)]"
            >
                <view
                    class="step_item"
                    v-for="(stepItem, stepIndex) in getComponentData(item.id)
                        .config.value"
                    :key="stepIndex"
                >
                    <view class="step_item_text">
                        <view class="step_item_text_left">
                            <u-text
                                align="right"
                                :text="stepItem.time"
                                :size="item.style.fontSize"
                                :color="item.style.color"
                            />
                        </view>
                        <view class="step_item_text_dot">
                            <view
                                class="step_item_text_dot_dot"
                                :style="{ backgroundColor: item.style.color }"
                            />
                            <view
                                v-if="
                                    stepIndex + 1 !==
                                    getComponentData(item.id).config.value
                                        .length
                                "
                                class="step_item_text_dot_line"
                                :style="{ backgroundColor: item.style.color }"
                            />
                        </view>
                        <view class="step_item_text_right">
                            <u-text
                                align="left"
                                :text="stepItem.text"
                                :size="item.style.fontSize"
                                :color="item.style.color"
                            />
                        </view>
                    </view>
                </view>
            </view>

            <!-- 表单 -->
            <view class="form" v-if="item.type === 'form'">
                <u-form>
                    <u-form-item>
                        <u-button
                            @click="reportHandler"
                            :text="
                                item.attrs && item.attrs.buttonDefaultText
                                    ? item.attrs.buttonDefaultText
                                    : '请设置按钮文字'
                            "
                            size="large"
                            :color="
                                item.attrs && item.attrs.buttonColor
                                    ? item.attrs.buttonColor
                                    : ''
                            "
                            :customStyle="{
                                width: '622rpx',
                                height: '102rpx',
                                color:
                                    item.attrs && item.attrs.buttonTextColor
                                        ? item.attrs.buttonTextColor
                                        : '#000',
                                borderRadius: '18rpx',
                            }"
                        />
                    </u-form-item>
                </u-form>
            </view>
        </view>
    </view>
</template>

<script>
import RecursiveComponent from "../RecursiveComponent/index.vue";
import { report } from "@/api/invitedCard.js";
export default {
    name: "RecursiveComponent",
    data() {
        return {
            mapLableFontSize: uni.upx2px(26),
            markerWidth: uni.upx2px(32),
            markerHeight: uni.upx2px(44),
        };
    },
    components: {
        RecursiveComponent,
    },
    props: {
        meetId: {
            type: String,
            default: "",
        },
        status: {
            type: String,
            default: "",
        },
        children: {
            type: Array,
            default: [],
        },
        fragmentStyle: {
            type: Object,
            default: {},
        },
        componentsData: {
            type: Object,
            default: null,
        },
    },
    mounted() {
        console.log(this.children);
    },
    computed: {
        getComponentData() {
            return function (componentId) {
                if (this.componentsData && this.componentsData.modules) {
                    return this.componentsData.modules.find(
                        (item) => item.id === componentId
                    );
                } else {
                    return null;
                }
            };
        },
    },
    methods: {
        async reportHandler() {
            if (this.status == 3) {
                uni.showToast({
                    icon: "none",
                    title: "预览时不能报名",
                });
                return;
            }
            const { code, msg } = await report({
                meetId: this.meetId,
            });
            if (code === 200) uni.$u.toast("报名成功！", 3000);
        },
        goMap({ latitude, longitude, name }) {
            uni.openLocation({
                latitude: Number(latitude),
                longitude: Number(longitude),
                name,
                scale: 28,
            });
        },
        preview(componentId, current) {
            // 根据组件id找到预览图片的模版数据
            const result = this.componentsData.modules.find(
                (item) => item.id === componentId
            );
            if (result.type === "images") {
                // 获取图片数组
                const urls = result.config.value.map((item) => item.url);
                // 调用预览api
                uni.previewImage({
                    urls,
                    current,
                });
            }
        },
    },
};
</script>

<style scoped lang="scss">
.rootDefaultStyle {
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
}

.step {
    display: flex;
    flex-direction: column;

    &_item {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;

        &_text {
            width: 100%;
            display: flex;
            &_left {
                width: 280rpx;
            }

            &_right {
                width: 280rpx;
            }

            &_dot {
                display: flex;
                flex-direction: column;
                align-items: center;
                margin: 0 40rpx;

                &_dot {
                    width: 30rpx;
                    height: 30rpx;
                    border-radius: 100px;
                }
                &_line {
                    width: 4rpx;
                    height: 96rpx;
                }
            }
        }
    }
}

.filed {
    display: flex;
    flex-direction: column;

    &_item {
        width: 100%;
        display: flex;
        justify-content: space-between;
        margin-bottom: 20rpx;
        text-align: left;

        &_line {
            width: 64rpx;
            height: 6rpx;
            margin-bottom: 12rpx;
        }
    }
}

.imagesScrollView {
    display: flex;
    flex-direction: column;

    &_item {
        margin-bottom: 36rpx;
    }
    .imagesScrollView_item:last-of-type {
        margin-bottom: 0;
    }
}

.map {
    position: relative;
    overflow: hidden;

    &_button {
        display: flex;
        justify-content: center;
        align-items: center;
        position: absolute;
        right: 32rpx;
        bottom: 40rpx;
        z-index: 10;
        width: 200rpx;
        height: 64rpx;
        background-color: #2254f4;
        border-radius: 8rpx;
    }
}

.line {
    width: 94rpx;
    height: 8rpx;
}
</style>
